﻿using CRM.Infrastructure.Dto.Request.Dictionary;
using CRM.Infrastructure.Dto.Request.products;
using CRM.Infrastructure.Dto.Response.product;
using CRM.Infrastructure.EFCore.Mysql;
using CRM.Infrastructure.IOC;
using CRM.Service.Interface;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace CRM.Api.Admin.Controllers
{
    public class ProductsController : BaseController
    {
        private readonly I_Products_Service _products_Service;
        private readonly string _uploadsPath;
        public ProductsController(I_Products_Service products_Service, I_Login_Service login_Service,IWebHostEnvironment env) : base(login_Service)
        {
            _products_Service = products_Service;
            _uploadsPath = Path.Combine(env.ContentRootPath, "uploads");
        }
        
        /// <summary>
        /// 获取产品类别列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Products_Types(int? page = 1, int? limit = 10, string? key = "")
        {
            var result = await _products_Service.Get_Products_Types(page ?? 1, limit ?? 10, key ?? "");
            return Ok(result);
        }
        /// <summary>
        /// 新增产品类别
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Roles = "admin")]
        public async Task<IActionResult> Add_Products_Types(string name)
        {
            var result = await _products_Service.Add_Products_Types(name);
            return Ok(result);
        }
        /// <summary>
        /// 获取产品类别下的产品
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Details_Products(string id,string? key="")
        {
            var result = await _products_Service.Details_Products(id,key??"");
            return Ok(result);
        }
        /// <summary>
        /// 获取单个产品
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Details_Products_single(string id)
        {
            var result = await _products_Service.Details_Products_single(id);
            return Ok(result);
        }
        /// <summary>
        /// 更换产品图片
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("{productId}")]
        public async Task<IActionResult> Upload(string productId, IFormFile file)
        {
            if (file == null || file.Length == 0)
                return BadRequest("No file uploaded.");

            var filePath = Path.Combine(_uploadsPath, file.FileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            // 更新产品的图片URL
            var result = await _products_Service.UpdateProductImageUrl(productId, $"/uploads/{file.FileName}");

            return Ok(result);
        }
        /// <summary>
        /// 更改产品名字及价格
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update_Product(Product_Response_Dto dto)
        {
            var result = await _products_Service.Update_Product(dto);
            return Ok(result);
        }
        /// <summary>
        /// 更改产品类型名字
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update_Product_Type(string id, string name)
        {
            var result = await _products_Service.Update_Product_Type(id, name);
            return Ok(result);
        }

        /// <summary>
        /// 删除产品
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Delete_Product(string id)
        {
            var result = await _products_Service.Delete_Product(id);
            return Ok(result);
        }
        /// <summary>
        /// 删除产品类别
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Delete_Product_Type(string id)
        {
            var result = await _products_Service.Delete_Product_Type(id);
            return Ok(result);
        }

        /// <summary>
        /// 新增产品
        /// </summary>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <param name="img"></param>
        /// <param name="type_id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add_product(Product_Response_Dto dto)
        {
            var result = await _products_Service.Add_product(dto);
            return Ok(result);
        }
        /// <summary>
        /// 选择产品图片
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> add_upload(IFormFile file)
        {
            if (file == null || file.Length == 0)
                return BadRequest("No file uploaded.");

            var filePath = Path.Combine(_uploadsPath, file.FileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }
            var result = await _products_Service.add_upload($"/uploads/{file.FileName}");
            return Ok(result);
        }
        /// <summary>
        /// 获取产品
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Products(string? key = "", int? page = 1, int? limit = 10)
        {
            var result = await _products_Service.Get_Products(key ?? "", page ?? 1, limit ?? 10);
            return Ok(result);
        }
        /// <summary>
        /// 获取产品类型
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Products_Types_Products()
        {
            var result = await _products_Service.Get_Products_Types_Products();
            return Ok(result);
        }
        /// <summary>
        /// 上架产品
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update_List(string id)
        {
            var result = await _products_Service.Update_List(id);
            return Ok(result);
        }
        /// <summary>
        /// 新增套餐
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add_Setmenu(Product_Response_Dto[] dto, string name, int list)
        {
            var result = await _products_Service.Add_Setmenu(dto,name,list);
            return Ok(result);
        }
        /// <summary>
        /// 获取套餐
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Setmenu(string? key = "", int? page = 1, int? limit = 10)
        {
            var result = await _products_Service.Get_Setmenu(key ?? "", page ?? 1, limit ?? 10);
            return Ok(result);
        }
        /// <summary>
        /// 获取套餐下的产品
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Setmenu_product(string id)
        {
            var result = await _products_Service.Get_Setmenu_product(id);
            return Ok(result);
        }

        /// <summary>
        /// 修改套餐
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update_Setmenu(Product_Response_Dto[] dto, string id, string name, int list)
        {
            var result = await _products_Service.Update_Setmenu(dto, id, name, list);
            return Ok(result);
        }
        /// <summary>
        /// 删除套餐
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Del_Setmenu(string id)
        {
            var result = await _products_Service.Del_Setmenu(id);
            return Ok(result);
        }


    }
}
